import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class War {
	private static Queue<String> A = new LinkedList<String>();
	private static Queue<String> B = new LinkedList<String>();
	private static Queue<String> spoilsOfWar = new LinkedList<String>();
	private static int steps, gameState = 0;
	private static boolean tied = false;
	private static Stack<String> cardHolder = new Stack<String>();

	public static void readFile() {
		int N = StdIn.readInt();
		while(!StdIn.isEmpty()){
			for(int i = 0; i < N * 2; i++){
				String temp = StdIn.readString();
				cardHolder.push(temp);
			}

			for(int i = 0; i < N; i++){
				String card1 = cardHolder.pop();
				B.add(card1);
			}
			for(int i = 0; i < N; i++){
				String card2 = cardHolder.pop();
				A.add(card2);
			}
		}
	}

	public static void war()
	{
		int cardBValue = 0, cardAValue = 0;
		String cardA1 = A.poll();
		String cardA2 = A.poll();
		String cardB1 = B.poll();
		String cardB2 = B.poll();
		if (cardA2 != null && cardB2 != null) {
			steps++;
			steps++;
			cardAValue = getValue(cardA2);
			cardBValue = getValue(cardB2);
			if(cardAValue == cardBValue){
				spoilsOfWar.add(cardA1);
				spoilsOfWar.add(cardB1);
				spoilsOfWar.add(cardA2);
				spoilsOfWar.add(cardB2);
				war();
			}
			else if(cardAValue > cardBValue){
				spoilsOfWar.add(cardA1);
				spoilsOfWar.add(cardB1);
				spoilsOfWar.add(cardA2);
				spoilsOfWar.add(cardB2);
				while(!spoilsOfWar.isEmpty()){
					A.add(spoilsOfWar.poll());
				}
			}
			else if(cardBValue > cardAValue){
				spoilsOfWar.add(cardA1);
				spoilsOfWar.add(cardB1);
				spoilsOfWar.add(cardA2);
				spoilsOfWar.add(cardB2);
				while(!spoilsOfWar.isEmpty()){
					B.add(spoilsOfWar.poll());
				}
			}
		}
		else if ((cardB2 == null && cardA2 == null) && (cardB1 != null && cardA1 != null)) {
			steps++;
			System.out.println("Game tied after "+steps+" steps!");
			tied = true;
		}
		else if (cardB2 == null && cardA1 != null) {
			while(!spoilsOfWar.isEmpty()){
				A.add(spoilsOfWar.poll());
			}
		}
		else if(cardB2 == null && cardA2 != null) {
			steps++;
			while(!spoilsOfWar.isEmpty()){
				A.add(spoilsOfWar.poll());
			}
		}
		else if (cardB2 != null && cardA1 == null) {
			while(!spoilsOfWar.isEmpty()){
				B.add(spoilsOfWar.poll());
			}
		}
		else if (cardB2 != null && cardA2 == null) {
			steps++;
			while(!spoilsOfWar.isEmpty()){
				B.add(spoilsOfWar.poll());
			}
		}
		else {
			System.out.println("Game tied after "+steps+" steps!");
			tied = true;
		}
	}

	public static int getValue(String card)
	{
		if(card.substring(0, 1).equals("A")) {
			return 1;
		}
		else if(card.substring(0, 1).equals("T")) {
			return 10;
		}
		else if(card.substring(0, 1).equals("J")) {
			return 11;
		}
		else if(card.substring(0, 1).equals("Q")) {
			return 12;
		}
		else	if(card.substring(0, 1).equals("K")) {
			return 13;
		}
		else {
			int value = Integer.parseInt(card.substring(0, 1));
			return value;
		}
	}

	public static void step()
	{
		String cardA = A.poll();
		int cardAValue = getValue(cardA);
		String cardB = B.poll();
		int cardBValue = getValue(cardB);
		steps++;
		if(cardAValue == cardBValue){
			spoilsOfWar.add(cardA);
			spoilsOfWar.add(cardB);
			war();
		}
		else if(cardAValue > cardBValue){
			A.add(cardA);
			A.add(cardB);
		}
		else if(cardAValue < cardBValue){
			B.add(cardA);
			B.add(cardB);
		}
	}




	/**
	 * Creates two queues 
	 */
	public static void main(String[] args) {
		readFile();
		
		while(!A.isEmpty() && !B.isEmpty() && steps < 100000)
		{
				step();
		}

		if(A.size() == 0 && !tied){
			gameState = 2;
		}
		else if(B.size() == 0 && !tied){
			gameState = 1;
		}

		if (gameState == 0) {
			System.out.println("Game tied after "+steps+" steps!");
		}
		else if (gameState == 1) {
			System.out.println("Player A wins after " + steps + " steps!");
		}
		else if (gameState == 2) {
			System.out.println("Player B wins after " + steps + " steps!");
		}
	}
}